<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>

	<head>
		<title>系统设置-角色管理</title>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/reset.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/style.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/iframe.css">
	</head>

	<body>
		<div id="mainContent">
			<div id="mainContent_content">
				<div class="page_header">
					<a>角色管理</a>
				</div>
				<div class="search">
					<form class="form" method="post" action="<%=basePath%>role/selectByWhere.do">
						<label> 角色名称</label>
						<input type="text" id="rolename" name="rolename" value="${rolePage.rolename}" class="form-control"></input>
						<input type="hidden" name="currentPage" value="1">

						<span class="btn">
					<input type="submit" value="查询" class="btn btn1"></input>
					<input type="button" value="清空" onclick="clearSearchParam();" class="btn btn2"></input>
						<c:if test="${isAdd }">
							<input type="button" value="新增" onclick="addrole();"  class="btn btn1"></input>
						</c:if>
						<input type="button" value="编辑" onclick="toEditSupplier();"  class="btn btn2"></input>
						<c:if test="${isDelete }">
							<input type="button" value="删除" onclick="toDeleteSupplier();" class="btn btn_danger"></input>
						</c:if>
					</span>

					</form>
				</div>

				<div class="tableBox box">
					<table class="table table-bordered table_text_center">
						<thead>
							<tr>
								<th></th>
								<th>操作</th>
								<th>角色名称</th>
								<th>角色状态</th>
							</tr>
						</thead>
						<tbody>
							<c:forEach items="${role }" var="role" varStatus="i">
								<tr onclick="selectRow(this);">
									<td>
										<label class="radio"><input type="radio" name="operateSupplier" value="${role.roleid}"><span class="radio_icon"></span></label>
									</td>
									<td>
										<a class="btn btn-xs btn1" onclick="updatesrole(${role.roleid });">编辑</a>
										<c:if test="${isDelete }">
											<a class="btn btn-xs btn_danger" onclick="confirmDeleteUser('${role.roleid }');">删除</a>
										</c:if>
										<c:if test="${role.status=='关闭' }">
											<a class="btn btn-xs btn2" onclick="updaterole('${role.roleid}','启用');">启用</a>
										</c:if>
										<c:if test="${role.status=='启用' }">
											<a class="btn btn-xs btn1" onclick="updaterole('${role.roleid}','关闭');">关闭</a>
										</c:if>
										<a class="btn btn-xs btn1" onclick="updaterole_jm('${role.roleid }');">管理角色权限</a>
									</td>
									<td>${role.rolename }</td>
									<td>${role.status }</td>
								</tr>
							</c:forEach>
						</tbody>
					</table>
				</div>
				<jsp:include page="/jsp/basic/page.jsp"></jsp:include>
			</div>
			<jsp:include page="/jsp/system/role/updaterole.jsp"></jsp:include>
			<jsp:include page="/jsp/system/role/addrole.jsp"></jsp:include>
			<jsp:include page="/jsp/basic/prompt.jsp"></jsp:include>
		</div>
		<script src="<%=basePath%>js/jquery-3.2.1.min.js"></script>
		<script src="<%=basePath%>libs/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
		<script src="<%=basePath%>js/public.js"></script>
		<script type="text/javascript">
			//清空搜索条件
			function clearSearchParam() {
				$(".search input:text").val("");
				$(".search select").val("");
			}

			function selectRow(obj) {
				$(obj).find('input:radio[name="operateSupplier"]')[0].checked = true;
			}
			//修改角色权限
			function updaterole_jm(id) {
				window.location.href = "<%=basePath%>/Jurisdiction/selectJurisdiction.do?roleid=" +
					id;
			}

			function toEditSupplier() {
				var radio = $('input:radio[name="operateSupplier"]:checked');
				if(!radio.val()) {
					Prompt.alert("提示", "请先选中需要编辑的角色！");
					return;
				}
				updatesrole(radio.val());
			}
			/* 启用关闭权限 */
			function updaterole(id, status) {
				var delFunc = function() {
					$.ajax({
						url: "<%=basePath%>role/updaterole.do",
						type: "post",
						async: false,
						dataType: "json",
						data: 'roleid=' + id + "&status=" + status,
						success: function(data) {
							if(data.result == "success") {
								Prompt.alert("提示", status + "成功");
								location.href = location.href;
							} else {
								Prompt.alert("提示", status + "失败！");
							}
						},
						error: function() {
							Prompt.alert("提示", "服务器无响应，请刷新页面后重试！");
						}
					});
				}
				Prompt.confirm("提示", "确认要" + status + "吗?", delFunc);
			}

			function toDeleteSupplier() {
				var radio = $('input:radio[name="operateSupplier"]:checked');
				if(!radio.val()) {
					Prompt.alert("删除提示", "请先选中需要删除的角色！");
					return;
				}
				confirmDeleteUser(radio.val());
			}

			function confirmDeleteUser(id) {
				var delFunc = function() {
					$.ajax({
						url: "<%=basePath%>role/delectrole.do",
						type: "post",
						async: false,
						dataType: "json",
						data: "id=" + id,
						success: function(data) {
							if(data.result == "success") {
								Prompt.alert("删除提示", "角色已删除！");
								location.href = location.href;
							} else {
								Prompt.alert("删除提示", "删除失败！");
							}
						},
						error: function() {
							Prompt.alert("删除提示", "服务器无响应，请刷新页面后重试！");
						}
					});
				}
				Prompt.confirm("提示", "确认要删除该角色吗?删除后无法恢复", delFunc);
			}
		</script>
	</body>

</html>